package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.EventBean;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.bean.LoginBean;
import com.example.hotel_lxy.bean.SqliteBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.sql.UserDao;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.ysxsoft.common_base.view.custom.image.RoundImageView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 切换账号
 */
@Route(path = "/main/SwitchAccountsActivity")
public class SwitchAccountsActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.rv_switch_list)
    RecyclerView rvSwitchList;
    @BindView(R.id.line_login)
    LinearLayout lineLogin;
    private UserDao demoDao;
    String id;

    public static void start() {
        ARouter.getInstance().build(ARouterPath.getSwitchAccountsActivity())
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_switch_accounts;
    }

    @Override
    public void doWork() {
        super.doWork();
        demoDao = new UserDao(this);
        back.setOnClickListener(this);
        lineLogin.setOnClickListener(this);
        id = SPUtils.get(SwitchAccountsActivity.this, "id", "").toString();
        query();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.line_login:
                 SPUtils.put(SwitchAccountsActivity.this,"token","");
                SPUtils.put(SwitchAccountsActivity.this, "id", "");
                Intent intent = new Intent(SwitchAccountsActivity.this,LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
                break;
        }
    }

    public void query() {
        List<SqliteBean> all = demoDao.getAll();
        if (all.size() != 0 || all != null) {
            rvSwitchList.setLayoutManager(new LinearLayoutManager(this));
            RBaseAdapter<SqliteBean> rBaseAdapter = new RBaseAdapter<SqliteBean>(this, R.layout.item_switch_activity, all) {
                @Override
                protected void fillItem(RViewHolder holder, SqliteBean item, int position) {
                    RoundImageView iv = holder.itemView.findViewById(R.id.iv);
                    Log.e("TAG",item.getAvatar()+"  123");
                    Glide.with(SwitchAccountsActivity.this).load(item.getAvatar()).placeholder(R.mipmap.icon_welcome).into(iv);
                    TextView text_mobile = holder.itemView.findViewById(R.id.text_mobile);
                    text_mobile.setText("" + item.getName());
                    TextView text_id = holder.itemView.findViewById(R.id.text_id);
                    TextView text1 = holder.itemView.findViewById(R.id.text1);
                    TextView text2 = holder.itemView.findViewById(R.id.text2);
                    TextView text3 = holder.itemView.findViewById(R.id.text3);
                    text1.setText(item.getIds());
                    text2.setText(item.getToken());
                    text3.setText(item.getMobile());
                    Log.e("TAG",item.getIds()+"   "+id);
                    if (id.equals("" + item.getIds())) {
                        text_id.setVisibility(View.VISIBLE);
                    } else {
                        text_id.setVisibility(View.GONE);
                    }
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("TAG",text1.getText().toString()+"   "+text_mobile.getText().toString());
                            SPUtils.put(SwitchAccountsActivity.this,"token",""+text2.getText().toString());
                            SPUtils.put(SwitchAccountsActivity.this, "id", ""+text1.getText().toString());
                            SPUtils.put(SwitchAccountsActivity.this, "mobile", ""+text3.getText().toString());
                            SPUtils.put(SwitchAccountsActivity.this, "names", ""+text_mobile.getText().toString());
                            EventBean eventBusBean=new EventBean();
                            eventBusBean.setAnInt(0);
                            EventBus.getDefault().post(eventBusBean); //普通事件发布
                            Intent intent = new Intent(mContext, MainActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
//                            LoginActivity.start();
                            finish();
                        }
                    });
                }
                @Override
                protected int getViewType(SqliteBean item, int position) {
                    return 0;
                }
            };
            rvSwitchList.setAdapter(rBaseAdapter);
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}